import { Effect, Reducer } from 'umi';
import { getCategory } from '@/services';
import { ICategory } from '@/types';

export interface CategoryModelState {
  archivesPage: ICategory[];
}

export interface CategoryModelType {
  namespace: 'category';
  state: CategoryModelState;
  effects: {
    getCategory: Effect;
  };
  reducers: {
    save: Reducer<CategoryModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<CategoryModelState>;
  };
}

const CategoryModel: CategoryModelType = {
  namespace: 'category',

  state: {
    archivesPage: [],
  },

  effects: {
    //文章分类
    *getCategory({ payload }, { call, put }) {
      const result = yield call(getCategory)
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            archivesPage: result.data,
          }
        })
      };
    },

  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },

};

export default CategoryModel;